import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class NGuessNumber {

    public static int[] generateAnswer(){
        Random random = new Random();
        int[] nums = new int[4];
        for (int i = 0; i < nums.length; i++) {
            int num = random.nextInt(10);
            if (!Arrays.toString(nums).contains(String.valueOf(num))){
                nums[i] = num;
            }else {
                i--;
            }

        }
        return nums;
    }


    public static int[] getPlayerInput(){
        int[] nums = new int[4];
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 4; i++) {
            System.out.println(String.format("输入第%d个数字:",i + 1));
            nums[i] = scanner.nextInt();
        }
        return nums;
    }

    public static int[] compareGuessAnswer(int[] nums_password , int[] nums_userinput){
        int[] count = new int[2];
        for (int i = 0; i < nums_password.length ; i++) {
            if (nums_password[i] == nums_userinput[i]){
                count[0]++;
            }else if(Arrays.toString(nums_password).contains(String.valueOf(nums_userinput[i]))){
                count[1]++;
            }

        }
        System.out.println(String.format("A:%d   B:%d",count[0],count[1]));
        return count;
    }

    public static boolean isWin(int[] count){
        return count[0] == 4;
    }


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        do{
            int[] nums_ps = generateAnswer();
            System.out.println(Arrays.toString(nums_ps));
            int max_num_count = 0;
            while (max_num_count < 5){
                int[] nums_in = getPlayerInput();
                int[] count = compareGuessAnswer(nums_ps,nums_in);
                if(isWin(count)){
                    System.out.println("游戏获胜");
                    break;
                }
                max_num_count++;
            }
            System.out.println("继续游戏输入y/n");
        }while (scanner.next().equals("y"));


    }




}
